package result

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// 分页响应
type PageResponse struct {
	Response
}

type PageInfo struct {
	Page      int         `json:"page"`
	PageSize  int         `json:"page_size"`
	Total     int64       `json:"total"`
	TotalPage int         `json:"total_page"`
	Data      interface{} `json:"data"`
}

// 分页成功响应
func PageSuccess(c *gin.Context, data interface{}, total int64, page, pageSize int) {
	totalPage := 0
	if total > 0 && pageSize > 0 {
		totalPage = int((total + int64(pageSize) - 1) / int64(pageSize))
	}

	c.JSON(http.StatusOK, PageResponse{
		Response: Response{
			Code:    SuccessCode,
			Message: "成功",
			Data: PageInfo{
				Page:      page,
				PageSize:  pageSize,
				Total:     total,
				TotalPage: totalPage,
				Data:      data,
			},
			Success: true,
		},
	})
}
